home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / H_SCROLL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  14.3 KB  |  466 lines

  1. package sub_arctic.lib;
  2. import sub_arctic.input.*;
  3. import sub_arctic.output.*;
  4. import sub_arctic.lib.sub_arctic_error;
  5.  
  6. /**
  7.  * This is a "style-free" horizontal scrollbar interactor. It works with
  8.  * the style_manager and a style object to get its style.  It provides 
  9.  * a variable sized thumb that represents how much of a whole is being 
  10.  * presented.
  11.  * 
  12.  * @author Ian Smith
  13.  */
  14. public class h_scrollbar extends h_slider {
  15.  
  16.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  17.  
  18.   /** 
  19.    * Indicate which values (coordinates/sizes) of this object are
  20.    * intrinsically constrained by the internals of the object.  In this case
  21.    * we need to remove PART_B from the set provided by the superclass.
  22.    */
  23.   public int intrinsic_constraints()
  24.     {
  25.       return super.intrinsic_constraints() & ~ PART_B;
  26.     }
  27.  
  28.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  29.  
  30.   /**
  31.    * Compute the offset to the thumb in pixels.  We override here so we never
  32.    * return -1 for the doesn't fit condition (which was for use by sliders
  33.    * that have a fixed size thumb), and just return 0 instead.
  34.    *
  35.    * @return int the amount the thumb should be shifted down to account for the 
  36.    *         value of the slider
  37.    */
  38.   protected int thumb_offset()
  39.     {
  40.       int result = super.thumb_offset();
  41.       if (result < 0) result = 0;
  42.       return result;
  43.     }
  44.  
  45.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  46.  
  47.   /**
  48.    * This holds the usable area of the scrollbar. 
  49.    */
  50.   protected int _usable_area;
  51.  
  52.   /**
  53.    * This computes the usable area of the scrollbar.
  54.    */
  55.   protected void compute_usable_area() {
  56.     loaded_image left=left_img();
  57.     loaded_image right=right_img();
  58.  
  59.     /* sanity check */
  60.     if ((left==null) || (right==null)) {
  61.       _usable_area=0;
  62.     }
  63.     /* normal case */
  64.     _usable_area=w()-(right.width()+left.width());
  65.   }
  66.  
  67.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  68.   
  69.   /** 
  70.    * Return the value of the part_b component of this object.  In our case 
  71.    * this is tied to _thumb_percentage.  In particular its 
  72.    * thumb_percentage*thumb_scale where thumb_percentage is in the range 
  73.    * 0..1. 
  74.    * 
  75.    * @return int the up-to-date value of part_b which in our case is the 
  76.    *             thumb_percentage.
  77.    */
  78.   public int part_b()
  79.     {
  80.       /* Make sure its up to date and then return it */
  81.       eval_part_b();
  82.       return (int)(((double)_thumb_percentage)*thumb_scale() + 0.5);
  83.     }
  84.  
  85.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  86.  
  87.   /** 
  88.    * Set part_b value directly bypassing the constraint system (but doing 
  89.    * damage).  This should normally only be done by the constraint system 
  90.    * or some other part of the system which takes care of marking things 
  91.    * out-of-date, etc. itself.  In this case part_b is tied to 
  92.    * _thumb_percentage.
  93.    *
  94.    * @param int v the new value for part_b.
  95.    */
  96.   protected void set_raw_part_b(int v) 
  97. {
  98.       double new_v;
  99.  
  100.       /* compute the floating point equivalent */
  101.       new_v = ((double)v)/thumb_scale();
  102.       if (new_v < 0.0) new_v = 0.0;
  103.       if (new_v > 1.0) new_v = 1.0;
  104.  
  105.       /* don't do anything unless this is a change */
  106.       if (new_v != _thumb_percentage)
  107.     {
  108.       /* make change */
  109.           _thumb_percentage = new_v;
  110.  
  111.           /* make sure we have the usable area and make a new image */
  112.           compute_usable_area();
  113.           calculate_thumb_image(); // this does damage_self();
  114.     }
  115.     }
  116.  
  117.    //had
  118.    //* @exception general
  119.  
  120.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  121.  
  122.   /** 
  123.    * Set the part_b component of this object.  In our case this is tied to 
  124.    * thumb_percentage. 
  125.    * @param int v the value new value of part_b.
  126.    */
  127.   public void set_part_b(int v) 
  128. {
  129.       double old_v;
  130.  
  131.       /* if this has a constraint throw an exception */
  132.       if ((active_constraints() & PART_B) != 0)
  133.         throw new sub_arctic_error(
  134.       "Attempt to assign value to constrained part_b " +
  135.       "(AKA h_scrollbar.thumb_percentage)");
  136.  
  137.       /* set the value */
  138.       old_v = _thumb_percentage;
  139.       set_raw_part_b(v);
  140.  
  141.       /* if its changed, mark it out of date */
  142.       if (old_v != _thumb_percentage) mark_part_b_ood();
  143.     }
  144.  
  145.    //had:
  146.    //* @exception cannot_assign if the part is constrained.
  147.    //* @exception general
  148.  
  149.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  150.  
  151.   /** 
  152.    * The amount we scale thumb_percentage (which is in the range 0..1) by when
  153.    * return it (or accept it) as an integer through the part_b interface.  
  154.    * This defaults to 100.0 which corresponds to integer values being one
  155.    * percent each (i.e., 0..100 is the legal range). 
  156.    */
  157.   protected double _thumb_scale = 100.0;
  158.  
  159.   /** 
  160.    * The amount we scale thumb_percentage (which is in the range 0..1) by when
  161.    * return it (or accept it) as an integer through the part_b interface.  
  162.    * This defaults to 100.0 which corresponds to integer values being one
  163.    * percent each (i.e., 0..100 is the legal range). 
  164.    *
  165.    * @see #thumb_percentage
  166.    * @return double the scale value.
  167.    */
  168.   public double thumb_scale() {return _thumb_scale; }
  169.  
  170.   /** 
  171.    * The amount we scale thumb_percentage (which is in the range 0..1) by when
  172.    * return it (or accept it) as an integer through the part_b interface.  
  173.    * This defaults to 100.0 which corresponds to integer values being one
  174.    * percent each (i.e., 0..100 is the legal range).  If you want more 
  175.    * resolution than this, set the scale to a larger number.  Values <= 0
  176.    * will restore the scale value to the default (100.0). 
  177.    *
  178.    * @see #thumb_percentage
  179.    * @param double v the new scale value.
  180.    */
  181.   public void thumb_scale(double v) 
  182.     {
  183.       if (v <= 0) v = 100.0; 
  184.       _thumb_scale = v; 
  185.      }
  186.  
  187.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  188.  
  189.   /**
  190.    * This is the amount as a percentage (0..1) of size the thumb takes up.
  191.    */
  192.   protected double _thumb_percentage;
  193.  
  194.   /**
  195.    * Retrieve the amount of space covered by the thumb.  This value is 
  196.    * tied to part_b of this object (which is accessible as a scaled integer
  197.    * via the constraint system).
  198.    * @see #thumb_scale
  199.    * @return double the size of the scrollbar as a percent (0..1) of the area.
  200.    */
  201.   public double thumb_percentage() 
  202.     {
  203.       /* Make sure its up to date and then return it */
  204.       eval_part_b();
  205.       return _thumb_percentage;
  206.     };
  207.  
  208.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  209.  
  210.   /** Size of current thumb (so we know if we need to replace it) */
  211.   protected int _cur_thumb_size = -1;
  212.  
  213.   /**
  214.    * This does the work of actually establish a proper thumb image.
  215.    */
  216.   public void calculate_thumb_image() {
  217.     int pixel_width;
  218.     int min=style_manager.current_style().h_scrollbar_minimum_thumb_size();
  219.  
  220.     /* what's the percentage */
  221.     pixel_width=Math.round((float)(((double)_usable_area)*
  222.                    thumb_percentage()));
  223.  
  224.     /* is it too small? */
  225.     if (pixel_width<min) {
  226.       pixel_width=min;
  227.     }
  228.  
  229.     /* if we have changed, build the thumb image */
  230.     if (pixel_width != _cur_thumb_size)
  231.       {
  232.         set_thumb_img(style_manager.current_style().
  233.           h_scrollbar_thumb(pixel_width));
  234.     _cur_thumb_size = pixel_width;
  235.       }
  236.   }
  237.  
  238.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  239.  
  240.   /**
  241.    * Set the percentage of the whole size the thumb is now occupying.  This 
  242.    * is tied to part_b where it is manipulated as a scaled integer.
  243.    * 
  244.    * @see #thumb_scale
  245.    * @see #part_b
  246.    * @param double the percentage of the size of the scrollbar the thumb 
  247.    *               is occupying (0.0 < this number  <= 1.0)
  248.    */
  249.   public void set_thumb_percentage(double d) {
  250.  
  251.  
  252.     /* force d in range */
  253.     if (d < 0.0) d = 0.0;
  254.     if (d > 1.0) d = 1.0;
  255.  
  256.     /* only do something if we have a change */
  257.     if (d==_thumb_percentage) return;
  258.  
  259.     /* do the assignment and mark part_b out of date as a result */
  260.     _thumb_percentage = d;
  261.     mark_part_b_ood();
  262.  
  263.     /* make sure we have the usable area and recompute image */
  264.     compute_usable_area();
  265.     calculate_thumb_image(); // this does damage_self();
  266.   }
  267.  
  268.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  269.  
  270.   /**
  271.    * Internal function to initialize the scrollbar.
  272.    */
  273.   protected void init() {
  274.     /* this is the same as a change */
  275.     style_changed();
  276.  
  277.       /* set our intrinsic width to the width of any of the images */
  278.  
  279.       /* sanity check first */
  280.       if (left_img()==null) {
  281.     set_intrinsic_h(10); // bogus but the user will want to see something
  282.       } else {
  283.     set_intrinsic_h(left_img().height());
  284.       }
  285.  
  286.       /* do we never want this? */
  287.       set_boxed(false);
  288.   }
  289.  
  290.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  291.  
  292.   /**
  293.    * Create a horizontal scrollbar at a given location on the screen with
  294.    * a given width.
  295.    *
  296.    * @param int             x       the x position of this object.
  297.    * @param int             y       the y position of this object.
  298.    * @param int             w       the width of this object.
  299.    * @param int             min     the min value of this object.
  300.    * @param int             max     the max value of this object.
  301.    * @param int             init    the initial value of this object.
  302.    * @param double          percent the size of the scrollbar (larger than 
  303.    *                                zero, less than or equal to one).
  304.    * @param int             small   the small increment value (which occurs 
  305.    *                                when you press a end button).
  306.    * @param int             large   the large increment value (which occurs 
  307.    *                                when you click in the thumb area).
  308.    * @param callback_object co      the object to send callbacks to (#0 is the 
  309.    *                                mouse has been released and the thumb is 
  310.    *                                stopped moving and #1 is the thumb is 
  311.    *                                being dragged).
  312.    */
  313.   public h_scrollbar(int x, int y, int w, int min, int max, int init,
  314.              double percent,int small, int large, 
  315.              callback_object co) {
  316.  
  317.     // temporarily construct a default vertical slider
  318.     super(x,y,w,min,max,init,small,large,co);
  319.  
  320.     /* put the percent in */
  321.     if (percent<0.0) {
  322.       _thumb_percentage=0.0;
  323.     } else if (percent>=1.0) {
  324.       _thumb_percentage=1.0;
  325.     } else {
  326.       _thumb_percentage=percent;
  327.     }
  328.  
  329.     /* make it happen */
  330.     init();
  331.   }
  332.  
  333.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  334.  
  335.   /**
  336.    * Create a horizontal scrollbar with some reasonable defaults.
  337.    * This makes the range of the slider 0-99 and sets the initial
  338.    * value at 0. It makes the small increment 5 and the large
  339.    * increment 25. It sets the thumb to be 25% of the size of the
  340.    * scrollbar
  341.    * 
  342.    * @param int             x  the x position of the interactor.
  343.    * @param int             y  the x position of the interactor.
  344.    * @param int             w  the width of the interactor.
  345.    * @param callback_object co the object to send callbacks to.
  346.    */
  347.   public h_scrollbar(int x, int y, int w, callback_object co ) {
  348.     super(x,y,w,0,99,0,5,25,co);
  349.  
  350.     /* set the percentage */
  351.     _thumb_percentage=0.25;
  352.  
  353.     /* set things up */
  354.     init();
  355.   }
  356.  
  357.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  358.  
  359.   /**
  360.    * Minimal constructor for a horizontal scrollbar. It assumes
  361.    * you are going to size it and position it with constraints 
  362.    * or do so directly.
  363.    * This makes the range of the slider 0-99 and sets the initial
  364.    * value at 0. It makes the small increment 5 and the large
  365.    * increment 25. It sets the thumb to be 25% of the size of the
  366.    * scrollbar
  367.    * 
  368.    * @param callback_object co the object to send callbacks to
  369.    */
  370.   public h_scrollbar(callback_object co ) {
  371.     super(0,0,50,0,99,0,5,25,co);
  372.  
  373.     /* set the percentage */
  374.     _thumb_percentage=0.25;
  375.  
  376.     /* set things up */
  377.     init();
  378.   }
  379.  
  380.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  381.  
  382.   /**
  383.    * This function gets called when the system's style resources
  384.    * change or we are getting initialized.
  385.    */
  386.   public void style_changed() {
  387.     style cs=style_manager.current_style();
  388.     loaded_image img[];
  389.  
  390.     /* make the images */
  391.     img=cs.h_scrollbar_images();
  392.  
  393.     /* set them into their places */
  394.     set_left_img(img[0]);
  395.     set_right_img(img[1]);
  396.     set_back_img(img[2]);
  397.  
  398.     /* recalculate the thumb */
  399.     compute_usable_area();
  400.     calculate_thumb_image();
  401.  
  402.     /* put the shift on.. */
  403.     set_thumb_shift(cs.h_scrollbar_thumb_shift());
  404.   }
  405.  
  406.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  407.  
  408.   /**
  409.    * We override configure so we can automatically update the
  410.    * size of the scrollbar thumb.
  411.    */
  412.   public void configure() {
  413.     int pixel_width;
  414.     int min=style_manager.current_style().h_scrollbar_minimum_thumb_size();
  415.  
  416.     /* if things start breaking, this might need to go */
  417.     super.configure();
  418.  
  419.     /* make sure we have the usable area right */
  420.     compute_usable_area();
  421.  
  422.     /**
  423.      * WARNING: This code is basically the same as the code in 
  424.      * calculate_thumb_image() but it cannot call anything which
  425.      * would result in a call to damage_self() since this
  426.      * function is called in response to that.
  427.      */
  428.     /* what's the percentage */
  429.     pixel_width=Math.round((float)(((double)_usable_area)*
  430.                    thumb_percentage()));
  431.  
  432.     /* is it too small? */
  433.     if (pixel_width<min) {
  434.       pixel_width=min;
  435.     }
  436.  
  437.     /* if we have changed, build the thumb image */
  438.     if (pixel_width != _cur_thumb_size)
  439.       {
  440.         _thumb_img = style_manager.current_style().
  441.           h_scrollbar_thumb(pixel_width);
  442.     _cur_thumb_size = pixel_width;
  443.       }
  444.   }
  445.  
  446.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  447.  
  448. }
  449.  
  450. /*=========================== COPYRIGHT NOTICE ===========================
  451.  
  452. This file is part of the subArctic user interface toolkit.
  453.  
  454. Copyright (c) 1996 Scott Hudson and Ian Smith
  455. All rights reserved.
  456.  
  457. The subArctic system is freely available for most uses under the terms
  458. and conditions described in 
  459.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  460. and appearing in full in the lib/interactor.java source file.
  461.  
  462. The current release and additional information about this software can be 
  463. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  464.  
  465. ========================================================================*/
  466.